﻿using System;
using System.Collections.Generic;
using System.Text;


using CMN.Common;
using CMN.Entity;
using CMN.BLL;
using CMN.Cache;
using System.IO;
namespace CMN.Extends.Job
{
    public class JobCheckImg:IJob
    {

        public void Execute(object obj)
        {
            //if (DateTime.Now.Hour > 10 || DateTime.Now.Hour < 22)
            //{
            //    System.Threading.Thread.Sleep(100000);
            //    return;
            //}
            try
            {
                QueryParam qp = new QueryParam();
                qp.ReturnFields = "*";
                qp.PageIndex = 1;
                qp.PageSize = 20;
                qp.Where = " Img!=''and Editor=''";
                qp.Orderfld = "adddate asc";
                int recordNum = 0;
                Module_Article article = new Module_Article();
                Module_ArticleEntity articleEntity = new Module_ArticleEntity();

                Extend_CollectContent cContent = new Extend_CollectContent();
                qp.IsReturnCount = false;
                List<Extend_CollectContentEntity> articles = cContent.List(qp, out recordNum);
                //AppLog.Write("articles", AppLog.LogMessageType.Debug);
                foreach (Extend_CollectContentEntity c in articles)
                {
                    if (c.TextContent.Length > 20 && c.Title.Length >= 1)
                    {
                        Normal_Channel channel = new Normal_Channel();
                        //AppLog.Write("channel", AppLog.LogMessageType.Debug);
                        Normal_ChannelEntity channelEntity = channel.Disp(c.ChannelId);
                        if (channelEntity != null)
                        {

                            Extend_CollectSetInfoEntity setInfo = CacheCollcetSet.GetSetInfo(c.CollectSetId);
                            //AppLog.Write("setInfo", AppLog.LogMessageType.Debug);
                            if (setInfo != null)
                            {
                                
                            }
                            else
                            {
                                AppLog.Write(string.Concat("执行更新内容任务配置为空：【", c.CollectSetId, c.Title, "】"), AppLog.LogMessageType.Debug);
                                // AppLog.Write("setInfo NUll" + c.CollectSetId.ToString(), AppLog.LogMessageType.Debug);
                            }
                            if (!string.IsNullOrEmpty(c.Img))
                            {
                                string path = Utils.GetServerPath() + c.Img;
                                if (File.Exists(path))
                                {
                                    c.Editor = "o";
                                    c.DataTable_Action = DataTableAction.Update;
                                    cContent.InsertUpdateDelete(c);
                                }
                                else
                                {
                                    CollectHelper chelper = new CollectHelper();
                                    Extend_CollectContentEntity content = new Extend_CollectContentEntity();
                                    content = chelper.GetContent(setInfo,c.SourceFrom);
                                    c.Editor = "o";
                                    c.Img = content.Img;
                                    c.DataTable_Action = DataTableAction.Update;
                                    cContent.InsertUpdateDelete(c);

                                }

                            }
                            else
                            {
                                c.Editor = "o";
                                c.DataTable_Action = DataTableAction.Update;
                                cContent.InsertUpdateDelete(c);
                            }
                           
                            

                        }
                        else
                        {
                            AppLog.Write(string.Concat("执行更新内容任务栏目为空：【", c.Id.ToString(), "】"), AppLog.LogMessageType.Debug);
                        }
                    }
                   
                  
                    System.Threading.Thread.Sleep(50);
                    // AppLog.Write( string.Concat("执行更新内容任务:" ,c.Id.ToString(),"--", c.Title), AppLog.LogMessageType.Debug);
                }
                if( articles.Count<1)
                    System.Threading.Thread.Sleep(100000);
            }
            catch( Exception ex)
            {
                AppLog.Write("执行更新内容任务"+ex.Message,AppLog.LogMessageType.Error,ex);
            }



        }

        public string GetImg(string content)
        {

            string _LocalFileUrl = Utils.GetFirstImg(content);
            if (string.IsNullOrEmpty(_LocalFileUrl))
            {
                return "";
            }
            try
            {
                System.Drawing.Image getImage = System.Drawing.Image.FromFile(Utils.GetMapPath(_LocalFileUrl));
                if (getImage.Width < 300)
                {
                    return _LocalFileUrl;
                }
                string SavePath = "/Upload/";
                SavePath = SavePath + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/";
                Utils.CreateDir(Utils.GetMapPath(SavePath));
                string fileType = _LocalFileUrl.Substring(_LocalFileUrl.LastIndexOf('.'));

                System.Guid guid = new Guid();
                guid = Guid.NewGuid();
                SavePath += guid.ToString();

                SavePath += fileType;
                string saveAllPath = Utils.GetMapPath(SavePath);
                ImageHelper imgHelper = new ImageHelper();
                System.Drawing.Image newImag = imgHelper.ScaleFixWidth(getImage, 300);
                newImag.Save(saveAllPath, System.Drawing.Imaging.ImageFormat.Jpeg);
                newImag.Dispose();
                return SavePath;
            }
            catch (Exception ex)
            {
                AppLog.Write(ex.Message, AppLog.LogMessageType.Error, ex);
                return "";
            }

        }
        public void Error(Exception e)
        {
            AppLog.Write("执行更新内容任务"+e.Message, AppLog.LogMessageType.Error,e);
        }
    }
}
